package com.hyjx.framework.common.tree;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 树形数据结构工具类
 *
 * @author dijian
 */
public class TreeUtil {
    /**
     * 日志工具
     */
    private static Logger logger = LoggerFactory.getLogger(TreeUtil.class);

    /**
     * 根据树形列表和树的根节点生成树形对象
     *
     * @param list     数据列表
     * @param rootcode 树的根节点
     * @param checked  选中节点代码拼加的字符串 格式为 code1,code2
     * @return
     */
    private static TreeObject makeTree(List<TreeObject> list, String rootcode, String checked) {
        TreeObject rootNode = null;
        TreeObject rootParentNode = null;
        Map<String, TreeObject> treeMap = new HashMap<String, TreeObject>();
        String root = rootcode;
        TreeObject parentNode = null;
        for (TreeObject obj : list) {
            if (checked != null && !"".equals(checked)) {
                if (checked.indexOf("," + obj.getId() + ",") != -1) {
                    obj.setChecked(true);
                    obj.setSelected(true);
                }
            }
            treeMap.put(obj.getId(), obj);
            if (root != null && root.equals(obj.getId())) {
                if (rootNode == null) {
                    rootNode = obj;
                } else if (rootNode.hasChild() == false) {
                    TreeObject temp = new TreeObject();
                    temp.addChild(rootNode);
                    temp.addChild(obj);
                    rootNode = temp;
                } else {
                    rootNode.addChild(obj);
                }
            } else if (rootNode == null && obj.getParent_id() != null && obj.getParent_id().equals(root)) {
                if (rootParentNode == null) {
                    rootParentNode = new TreeObject();
                }
                rootParentNode.addChild(obj);
            } else {
                parentNode = treeMap.get(obj.getParent_id());
                if (parentNode == null) {
                    parentNode = getParentNode(list, obj.getParent_id());
                }
                if (parentNode != null) {
                    parentNode.addChild(obj);
                }
            }
        }
        for (TreeObject obj : list) {
            if (obj.getCode() != null && obj.getCode().equals(rootcode))
                obj.setState("open");
            if (obj.hasChild()) {
            } else {
                obj.setState("open");
            }
        }
        if (rootNode == null) {
            rootNode = rootParentNode;
        }
        treeMap = null;
        return rootNode;
    }

    /**
     * 整理数据结构,改造为树型结构
     *
     * @param list
     * @param rootId
     * @param checked
     * @return
     */
    public static List<ZTreeNode> makeZTree(List<ZTreeNode> list, String rootId, String checked) {
        Map<String, ZTreeNode> treeMap = new HashMap<String, ZTreeNode>();
        Collections.sort(list);
        checked = "," + checked + ",";
        ZTreeNode rootNode = null;

        for (ZTreeNode node : list) {
            treeMap.put(node.getId(), node);
            String pId = node.getpId();

            if (checked.indexOf("," + node.getId() + ",") != -1) {
                node.setChecked(true);
            }

            if (rootId.equals(pId)) {
                rootNode = node;
            } else {
                if (treeMap.get(pId) != null) {
                    treeMap.get(pId).getChildren().add(node);
                }
            }
        }

        List<ZTreeNode> result = new ArrayList<ZTreeNode>();
        result.add(rootNode);
        return result;
    }

    /**
     * 根据根节点代码获取数据列表中的所有根节点
     *
     * @param list
     * @param parentId
     * @return
     */
    public static TreeObject getParentNode(List<TreeObject> list, String parentId) {
        for (TreeObject obj : list) {
            if (obj.getId().equals(parentId)) {
                return obj;
            }
        }
        return null;
    }

    /**
     * 根据树形列表和树的根节点生成easyui需要的json数据结
     *
     * @param list     数据列表
     * @param rootcode 根节点
     * @param checked  选中节点代码拼加的字符串 格式为 code1,code2,
     * @return
     */
    public static String getEasyuiJsonStr(List<TreeObject> list, String rootcode, String checked) {
        TreeObject rootNode = makeTree(list, rootcode, checked);
        String str = getEasyuiJsonStr(rootNode);
        return str;
    }

    /**
     * 根据树结构数据对象生成easyui需要的json数据结
     *
     * @param treeObject
     * @return
     */
    public static String getEasyuiJsonStr(TreeObject treeObject) {
        if (treeObject == null) {
            return "[]";
        }
        if (treeObject.getText() == null) {
            return JSONArray.fromObject(treeObject.getChildren()).toString();
        }
        return "[" + JSONObject.fromObject(treeObject).toString() + "]";
    }

    /**
     * 测试方法
     *
     * @param args
     */
    public static void main(String[] args) {
        List<TreeObject> list = new ArrayList<TreeObject>();
        TreeObject rootNode = new TreeObject();
        rootNode.setId("01");
        rootNode.setText("节点01");
        rootNode.setParent_id("#");
        list.add(rootNode);
        rootNode = new TreeObject();
        rootNode.setId("02");
        rootNode.setText("节点02");
        list.add(rootNode);
        rootNode = new TreeObject();
        rootNode.setId("0101");
        rootNode.setText("节点0101");
        rootNode.setParent_id("01");
        list.add(rootNode);
        rootNode = new TreeObject();
        rootNode.setId("0102");
        rootNode.setText("节点0102");
        rootNode.setParent_id("01");
        list.add(rootNode);

        rootNode = TreeUtil.makeTree(list, "01", null);
        String str = getEasyuiJsonStr(rootNode);
        logger.debug(str);
    }

    /**
     * 获得部门结构的easyui格式的json数据
     *
     * @param list     部门列表
     * @param rootcode 根节点
     * @param checked  选中节点代码拼加的字符串 格式为 code1,code2,
     * @return
     */
    public static String getDepJsonStr(List<TreeObject> list, String rootcode, String checked) {
        TreeObject rootNode = makeTree(list, rootcode, checked);
        String str = getDepJsonStr(rootNode);
        return str;
    }

    /**
     * 通过树结构对象获得部门结构的easyui格式的json数据
     *
     * @param treeObject
     * @return
     */
    public static String getDepJsonStr(TreeObject treeObject) {
        if (treeObject == null) {
            return "[]";
        }
        if (treeObject.getText() == null) {
            JSONArray jsonData = JSONArray.fromObject(treeObject.getChildren().get(0));
            return jsonData.toString();
        }
        return "[" + JSONObject.fromObject(treeObject).toString() + "]";
    }
}
